package flc.ast.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.util.BannerUtils;

import java.text.SimpleDateFormat;
import java.util.List;

import flc.ast.R;
import stark.common.basic.view.container.StkLinearLayout;

public class MyBannerAdapter extends BannerAdapter<String, MyBannerAdapter.ImageTitleHolder> {

    private Context context;

    public MyBannerAdapter(List<String> datas, Context context) {
        super(datas);
        this.context = context;
    }

    private int selPosition = 0;

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    public ImageTitleHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new ImageTitleHolder(BannerUtils.getView(parent, R.layout.item_banner));
    }

    @Override
    public void onBindView(ImageTitleHolder holder, String data, int position, int size) {
        Glide.with(holder.itemView).load(data).into(holder.ivBannerItemImg);
        holder.tvBannerItemDate.setText(TimeUtils.millis2String(FileUtils.getFileLastModified(data), new SimpleDateFormat("yyyy年MM月dd日")));
        if (position == selPosition) {
            holder.tvBannerItemDate.setVisibility(View.VISIBLE);
            holder.ivBannerItemDelete.setVisibility(View.VISIBLE);
            holder.ivBannerItemDownload.setVisibility(View.VISIBLE);
            holder.llBannerItemImg.setAlpha(1f);
        } else {
            holder.tvBannerItemDate.setVisibility(View.INVISIBLE);
            holder.ivBannerItemDelete.setVisibility(View.INVISIBLE);
            holder.ivBannerItemDownload.setVisibility(View.INVISIBLE);
            holder.llBannerItemImg.setAlpha(0.28f);
        }
        holder.ivBannerItemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener == null) return;
                mListener.onClickDelete(data, position);
            }
        });
        holder.ivBannerItemDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener == null) return;
                mListener.onClickDownload(data, position);
            }
        });
    }

    public class ImageTitleHolder extends RecyclerView.ViewHolder {
        public ImageView ivBannerItemImg, ivBannerItemDelete, ivBannerItemDownload;
        public TextView tvBannerItemDate;
        private StkLinearLayout llBannerItemImg;

        public ImageTitleHolder(@NonNull View view) {
            super(view);
            ivBannerItemImg = view.findViewById(R.id.ivBannerItemImg);
            ivBannerItemDelete = view.findViewById(R.id.ivBannerItemDelete);
            ivBannerItemDownload = view.findViewById(R.id.ivBannerItemDownload);
            tvBannerItemDate = view.findViewById(R.id.tvBannerItemDate);
            llBannerItemImg = view.findViewById(R.id.llBannerItemImg);
        }
    }

    public interface OnItemClickListener {
        void onClick(String path, int position);

        void onClickDelete(String path, int position);

        void onClickDownload(String path, int position);
    }

    private OnItemClickListener mListener;

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }
}
